package com.mw.wmimobile.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.mw.wmimobile.datastorage.ClassesDto;
import com.mw.wmimobile.datastorage.StaffDto;

public class MyDatabase extends  SQLiteOpenHelper {

	private static final String DATABASE_NAME = "WMIMobi.db";
	private static final int DATABASE_VERSION = 1;
	//prac table
	private static final String PRAC_PREF = "PRACOWNICY_PREF";
	//prac columns
	private static final String PRAC_ID ="ID";
	private static final String PRAC_NAME ="NAME";
	private static final String PRAC_EMAIL ="EMAIL";
	private static final String PRAC_DEGREE ="DEGREE";
	private static final String PRAC_PHONE ="PHONE";
	private static final String PRAC_ROOM ="ROOM";
	
	
	private static final String CREATE_TABLE_PRACOWNICY_PREF = 
			  "CREATE TABLE IF NOT EXISTS "+PRAC_PREF+"("
			+ PRAC_ID+" TEXT UNIQUE, "
			+ PRAC_NAME+" TEXT, "
			+ PRAC_EMAIL +" TEXT, "
			+ PRAC_DEGREE +" TEXT, "
			+ PRAC_PHONE +" TEXT, "
			+ PRAC_ROOM + " TEXT);";
	
	
	//zajecia 
	private static final String ZAJ_PREF = "ZAJECIA_PREF";
	//zajecia colums
	private static final String ZAJ_ID ="ID";
	private static final String ZAJ_CODE ="CODE";
	private static final String ZAJ_WEEKDAY ="WEEKDAY";
	private static final String ZAJ_TEACHER ="TEACHER";
	private static final String ZAJ_NAME ="NAME";
	private static final String ZAJ_STARTTIME ="START";
	private static final String ZAJ_GROUP ="ZAJ_GROUP";
	private static final String ZAJ_ROOM ="ROOM";
	
	
	private static final String CREATE_TABLE_ZAJ_PREF = 
			  "CREATE TABLE IF NOT EXISTS "+ZAJ_PREF+"("
			+ ZAJ_ID+" TEXT UNIQUE, "
			+ ZAJ_CODE+" TEXT, "
			+ ZAJ_WEEKDAY +" TEXT, "
			+ ZAJ_TEACHER +" TEXT, "
			+ ZAJ_NAME +" TEXT, "
			+ ZAJ_STARTTIME +" TEXT, "
			+ ZAJ_GROUP +" TEXT, "
			+ ZAJ_ROOM + " TEXT);";
	
	public MyDatabase(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_TABLE_PRACOWNICY_PREF);
		db.execSQL(CREATE_TABLE_ZAJ_PREF);
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL(CREATE_TABLE_PRACOWNICY_PREF);
		db.execSQL(CREATE_TABLE_ZAJ_PREF);
	}
	
	///METODY DLA PRACOWNIKOW PREFEROWANYCH
	
	public void addStaff(StaffDto sDTO) {
		ContentValues values = new ContentValues();
		values.put(PRAC_ID, sDTO.getId());
		values.put(PRAC_NAME, sDTO.getName());
		values.put(PRAC_EMAIL, sDTO.getEmail());
		values.put(PRAC_DEGREE, sDTO.getDegree());
		values.put(PRAC_PHONE, sDTO.getPhone());
		values.put(PRAC_ROOM, sDTO.getRoom());

		SQLiteDatabase db = this.getWritableDatabase();

		db.insert(PRAC_PREF, null, values);
		db.close();
	}

	//ID, NAME,EMAIL, DEGREE, PHONE, ROOM
	public List<StaffDto> getStaff() {
		List<StaffDto> result = new ArrayList<StaffDto>();
		String query = "SELECT  * FROM " + PRAC_PREF;

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(query, null);

		if (cursor.moveToFirst()) {
			do {
				StaffDto sDTO = new StaffDto();
				sDTO.setId(cursor.getString(0));
				sDTO.setName(cursor.getString(1));
				sDTO.setEmail(cursor.getString(2));
				sDTO.setDegree(cursor.getString(3));
				sDTO.setPhone(cursor.getString(4));
				sDTO.setRoom(cursor.getString(5));
				
				result.add(sDTO);

			} while (cursor.moveToNext());
		}

		return result;
	}

	
	
	public void deleteStaff(StaffDto sDTO) {

		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(PRAC_PREF, PRAC_ID + " = \"" + sDTO.getId() + "\"",
				null);
		db.close();

		Log.d("database", "ive deleted: " + sDTO.toString());

	}
	
	///METODY DLA ZAJEC PREFEROWANYCH
//	+ ZAJ_ID+" TEXT UNIQUE, "
//	+ ZAJ_CODE+" TEXT, "
//	+ ZAJ_WEEKDAY +" TEXT, "
//	+ ZAJ_TEACHER +" TEXT, "
//	+ ZAJ_NAME +" TEXT, "
//	+ ZAJ_STARTTIME +" TEXT, "
//	+ ZAJ_GROUP +" TEXT, "
//	+ ZAJ_ROOM + " TEXT);";
	
	public void addClass(ClassesDto cDTO) {
		ContentValues values = new ContentValues();
		values.put(ZAJ_ID, cDTO.getId());
		values.put(ZAJ_CODE, cDTO.getCode());
		values.put(ZAJ_WEEKDAY, cDTO.getWeekday());
		values.put(ZAJ_TEACHER, cDTO.getTeacher());
		values.put(ZAJ_NAME, cDTO.getName());
		values.put(ZAJ_STARTTIME, cDTO.getStartTime());
		values.put(ZAJ_GROUP, cDTO.getGroup());
		values.put(ZAJ_ROOM, cDTO.getRoom());
		
		SQLiteDatabase db = this.getWritableDatabase();

		db.insert(ZAJ_PREF, null, values);
		db.close();
	}

	//ID, NAME,EMAIL, DEGREE, PHONE, ROOM
	public List<ClassesDto> getClasses() {
		List<ClassesDto> result = new ArrayList<ClassesDto>();
		String query = "SELECT  * FROM " + ZAJ_PREF;

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(query, null);

		if (cursor.moveToFirst()) {
			do {
				ClassesDto cDTO = new ClassesDto();
				cDTO.setId(cursor.getString(0));
				cDTO.setCode(cursor.getString(1));
				cDTO.setWeekday(cursor.getString(2));
				cDTO.setTeacher(cursor.getString(3));
				cDTO.setName(cursor.getString(4));
				cDTO.setStartTime(cursor.getString(5));
				cDTO.setGroup(cursor.getString(6));
				cDTO.setRoom(cursor.getString(6));		
				
				result.add(cDTO);

			} while (cursor.moveToNext());
		}

		return result;
	}

	public void deleteClass(ClassesDto cDTO) {

		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(ZAJ_PREF, ZAJ_ID + " = \"" + cDTO.getId() + "\"",
				null);
		db.close();

		Log.d("database", "ive deleted: " + cDTO.toString());

	}
	
}